package com.android.androidintellectguess;

import java.util.Random;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.graphics.Typeface;
import android.os.CountDownTimer;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Chronometer;
import android.widget.TextView;

public class Utils{
	
	public final static String CONGRATS= "BINGO";
	public final static String TAG = "LinhNT";
	public final static String INVALID_NO = "Invalid number";
	public final static String NORMAL ="NORMAL MODE";
	public final static String ATTACK ="ATTACK MODE";
	public final static String SURVIVAL ="SURVIVAL MODE";
	public final static String REMAIN = "Remains:";
	public final static String NOOFTRIES = "Number of tries:";
	public final static String BUTTON_CANCEL = "Cancel";
	public final static String BUTTON_OK = "OK";
	public final static String MSG_LOST = "Your current game will be lost. Are you sure?";
	public final static String COMINGSOON = "This feature will be coming soon!!! I promised";
	public final static String STAYWITHME = "Please stay with me....";
	public static final int REQUEST_CODE = 10;
	public final static String NUMBEROFDIGITS = "Number of digits";
	public final static String CHANGEDIGITS = "Change digits";
	public final static String NEWGAME = "New Game";
	public final static String PREFS_NAME = "MyPrefsFile";
	public final static String PLAYAGAIN = "PLAY AGAIN";
	public final static String FONTDEFAULT = "RockSalt.ttf";
	public final static String CORRECTNO = "No: ";
	public final static long LIMITTIME = 60000L;
	
	public static void changebg(View v){
		v.setOnClickListener(new OnClickListener() {
			boolean isClick = true;
			boolean isNeutral = false;
			@Override
			public void onClick(View v) {
				if (isClick == true && isNeutral==false){
					v.setBackgroundResource(R.drawable.accepted);
					isClick= false;
				} else if (isClick == false && isNeutral==false){
					v.setBackgroundResource(R.drawable.cancel);
					isClick= true;
					isNeutral = true;
				} else if (isClick == true && isNeutral==true){
					v.setBackgroundResource(R.drawable.neutral);
					isClick= true;
					isNeutral=false;
				}
			}
		});
	}
	
	public static String convertSecondsToStringFormat(long totalsecs){
		long hours = (totalsecs/3600) ;
		long minutes = (totalsecs % 3600) / 60 ;
		long seconds = totalsecs % 60;
		
		String hour = (hours == 0)? "00": (hours <10)? "0" + hours:"" + hours;
		String minute = (minutes ==0)? "00":(minutes <10)? "0" + minutes:"" + minutes;
		String second = (seconds ==0)? "00":(seconds <10)? "0" + seconds:"" + seconds;

		return hour + ":" + minute + ":" + second;
	}
	
	public static int convertStringToSeconds(String str){
		 // Legacy code:
		int totalseconds=0;
		StringTokenizer st = new StringTokenizer(str, ":");
		while (st.hasMoreTokens()) {
			if (st.countTokens() ==3){
				totalseconds = Integer.parseInt(st.nextToken()) * 3600
						+ Integer.parseInt(st.nextToken()) * 60
						+ Integer.parseInt(st.nextToken()); 
			}else if (st.countTokens() ==2){
				totalseconds = Integer.parseInt(st.nextToken()) * 60
						+ Integer.parseInt(st.nextToken()); 
			}else{
				totalseconds = Integer.parseInt(st.nextToken());
			}
		}
		Log.i(TAG, "total=" + totalseconds);
		return totalseconds;
	}
//	
	
	
	public static void applyFonts(final View v, Typeface fontToSet)
	{
	    try {
	        if (v instanceof ViewGroup) {
	            ViewGroup vg = (ViewGroup) v;
	            for (int i = 0; i < vg.getChildCount(); i++) {
	                View child = vg.getChildAt(i);
	                applyFonts(child, fontToSet);
	            }
	        } else if (v instanceof TextView) {
	            ((TextView)v).setTypeface(fontToSet);
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	        // ignore
	    }
	}
	
	public static boolean compareHighScore(String oldValue, String newValue){
		if (convertStringToSeconds(oldValue) ==0) return true;
		else if (convertStringToSeconds(oldValue) > convertStringToSeconds(newValue) ) return true;
		else return false;
	}
	
	public static Editable removeSpecialChar(Editable s, String pattern) {
		int len = s.length();
		Matcher m = Pattern.compile(pattern).matcher(s);
		len = s.toString().length();
		if (!m.matches() || !avoidDup(s, len)) {
			if (len > 0) {
				if (len == 1) {
					s.clear();
				} else {
					s = s.delete(len - 1, len);
				}
			}
		}
		return s;
	}
	
	private static boolean avoidDup(Editable s, int len) {
		if (len > 0) {
			char last = s.charAt(len - 1);
			if (s.toString().indexOf(last) == s.toString().lastIndexOf(last))
				return true;
			else
				return false;
		}
		return true;
	}
	
	//In Normal Mode
	public static String respond(String str, String gnumber, Chronometer timer, CountDownTimer Countdowntimer) {
		String result = "";
		boolean isCorrect = true;
		for (int i = 0; i < gnumber.length(); i++) {
			char tmp = gnumber.charAt(i);
			int test = str.indexOf(tmp);
			if (test > -1) {
				if (test == i) {
					result = "<font color='yellow'>" + "V" + "</font>" + result;
					isCorrect = isCorrect && true;
				} else{
					result += "<font color='blue'>" + "O" + "</font>" ;
					isCorrect= isCorrect && false;
				}
			} else {
				result += "";
				isCorrect= isCorrect && false;
			}
		}
		if (isCorrect) {
			if (timer !=null)
				timer.stop();
			if (Countdowntimer !=null)
				Countdowntimer.cancel();
//			timer.setBase(SystemClock.elapsedRealtime());
//			Log.i(TAG, "Number of threads after all = " + Thread.activeCount());
			return Utils.CONGRATS;
		}
		return result;
	}
	
	public static String generateNumber(int len) {
		String str = "";
		Random r = new Random();
		do {
			String tmp = r.nextInt(10) + "";
			if (str.indexOf(tmp) < 0) {
				str = str + tmp;
			}
		} while (str.length() < len);
		Log.i("LinhNT", "generated number=" + str);
		return str;
	}
}